package com.dragee.modules.work.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dragee.common.R;
import com.dragee.modules.work.entity.OrderGroupEntity;
import com.dragee.modules.work.service.OrderGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;



/**
 * 拼团
 *
 * @author tc
 * @email tc@mail.com
 * @date 2023-07-08 21:23:17
 */
@RestController
@RequestMapping("work/ordergroup")
@Api(tags = "拼团")
public class OrderGroupController {
    @Autowired
    private OrderGroupService orderGroupService;

    /**
     * 列表
     */
    @GetMapping("groupList")
    @ApiOperation("未成团列表")
    public R list(@RequestParam("projectId") String projectId, String status){
        status = StrUtil.isBlank(status) ? "未成团" : status;
        List<OrderGroupEntity> datas = orderGroupService.list(new QueryWrapper<OrderGroupEntity>().select("groupId", "needNum", "customer")
                .eq("projectId", projectId)
                .eq("status", status)
                .eq("roleName", "团长"));
        List<JSONObject> resultDatas = new ArrayList<>();
        for (OrderGroupEntity data : datas) {
            JSONObject jo = new JSONObject();
            jo.set("groupId", data.getGroupId());
            jo.set("needNum", data.getNeedNum());
            jo.set("customer", data.getCustomer());
            List<OrderGroupEntity> groups = orderGroupService.list(new QueryWrapper<OrderGroupEntity>()
                    .eq("groupId", data.getGroupId()));
            jo.set("persons", groups);
            resultDatas.add(jo);
        }
        return R.ok().put("data", resultDatas);
    }





}
